/**
 * 加载所有省份
 */
function loadAllProvince() {
	var url = CTX + "/shop/SysParam/findAllProvince.do";
	$.ajax({
		type : "post",
		url : url,
		data : "",
		dataType : "json",
		success : function(json) {
			provincesReconstruct(json);
		}
	});
}
/**
 * 重新构造省份
 * @param list
 */
function provincesReconstruct(list) {
	var provinces = $("#provinces");
	var PROVINCE_ID = $("#provinceId").val();
	provinces.html("");
	provinces.append("<option value='' " + ">" + "全国</option>");
	for ( var i = 0; i < list.length; i++) {
		if (PROVINCE_ID != null && PROVINCE_ID != "" && PROVINCE_ID > 0
				&& PROVINCE_ID == list[i].PROVINCE_ID) {
			provinces.append("<option selected='selected' value='"
					+ list[i].PROVINCE_ID + "'>" + list[i].PROVINCE_NAME
					+ "</option>");
		} else {
			provinces.append("<option value='" + list[i].PROVINCE_ID + "'>"
					+ list[i].PROVINCE_NAME + "</option>");
		}
	}
	if((PROVINCE_ID == null || PROVINCE_ID == "") && list.length > 0){
		PROVINCE_ID = list[0].PROVINCE_ID;
	}	
	getCitiesByProvinceId(PROVINCE_ID);
}
/**
 * 根据provinceId得到所有地市
 */
function getCitiesByProvinceId(provinceId) {
	var url = CTX + "/shop/SysParam/findCityByProvince.do?provinceId="
			+ provinceId;
	$.ajax({
		type : "post",
		url : url,
		data : "",
		dataType : "json",
		success : function(json) {
			citiesReconstruct(json);
			getRegionsByCityId("");
		}
	});

}
/**
 * 重新构造地市
 * @param list
 */
function citiesReconstruct(list) {
	var cities = $("#cities");
	var CITY_ID = $("#cityId").val();
	cities.html("");
	cities.append("<option value='' " + ">" + "全省</option>");
	if (list.length == 1) {
		cities.append("<option selected='selected' value='" + list[0].CITY_ID + "'>"
				+ list[0].CITY_NAME + "</option>");
		getRegionsByCityId(list[0].CITY_ID);
	} else {
		for ( var i = 0; i < list.length; i++) {
			if (CITY_ID != null && CITY_ID != "" && CITY_ID > 0
					&& CITY_ID == list[i].CITY_ID) {
				cities.append("<option selected='selected' value='"
						+ list[i].CITY_ID + "'>" + list[i].CITY_NAME
						+ "</option>");
			} else {
				
				cities.append("<option value='" + list[i].CITY_ID + "'>"
						+ list[i].CITY_NAME + "</option>");
			}
		}
		if((CITY_ID == null || CITY_ID == "") && list.length > 0){
			CITY_ID = list[0].CITY_ID;
		}	
		getRegionsByCityId(CITY_ID);
	}
}
/*通过cityId获取区域*/
function getRegionsByCityId(cityId){
	var url = CTX + "/shop/SysParam/findRegionByCity.do?cityId="
	+ cityId;
	$.ajax({
		type : "post",
		url : url,
		data : "",
		dataType : "json",
		success : function(json) {
			regionsReconstruct(json);
}
});
}
/*重组区域*/
function regionsReconstruct(list) {
	var regions = $("#regions");
	var REGION_ID = $("#regionId").val();
	regions.html("");
	regions.append("<option value='' " + ">" + "全市</option>");
	if (list.length == 1) {
		regions.append("<option value='" + list[0].REGION_ID + "'>"
				+ list[0].REGION_NAME + "</option>");
	} else {
		for ( var i = 0; i < list.length; i++) {
			if (REGION_ID != null && REGION_ID != "" && REGION_ID > 0
					&& REGION_ID == list[i].REGION_ID) {
				regions.append("<option selected='selected' value='"
						+ list[i].REGION_ID + "'>" + list[i].REGION_NAME
						+ "</option>");
			} else {
				
				regions.append("<option value='" + list[i].REGION_ID + "'>"
						+ list[i].REGION_NAME + "</option>");
			}
		}
		
	}
}
